package top.mhtools.pigeon.common.factory.wechat;

import top.mhtools.pigeon.common.enums.DingMessageTypeEnum;
import top.mhtools.pigeon.common.enums.SendTypeEnum;
import top.mhtools.pigeon.common.factory.Message;
import top.mhtools.pigeon.common.persistence.SendMessageReqDTO;
import top.mhtools.pigeon.common.persistence.SendMessageRequest;
import top.mhtools.pigeon.common.persistence.SimpleSendMessageReqDTO;
import top.mhtools.pigeon.wechat.dto.WeChatMessageReqDTO;

import java.util.Optional;

/**
 * 企业微信机器人产品类
 *
 * @author machen
 * @date 2020/5/27 20:36
 */
public class WeChatMessage implements Message {

    @Override
    public SendMessageRequest buildMessage(SimpleSendMessageReqDTO simpleMessage) {
        SendMessageReqDTO sendMessageReqDTO = new SendMessageReqDTO();
        WeChatMessageReqDTO weChatMessageReqDTO = new WeChatMessageReqDTO();

        // 企业微信机器人人入参赋值
        weChatMessageReqDTO.setToken(simpleMessage.getToken());
        weChatMessageReqDTO.setMsgtype(DingMessageTypeEnum.MD.getName());
        weChatMessageReqDTO.setTemplateId(
                Optional.ofNullable(simpleMessage.getTemplateId()).orElse(""));
        weChatMessageReqDTO.setAtMobiles(simpleMessage.getSendTo());

        // 企业微信机器人消息体MD入参赋值
        WeChatMessageReqDTO.Markdown markdown = new WeChatMessageReqDTO.Markdown();
        markdown.setContent(simpleMessage.getContent());
        markdown.setTextMap(simpleMessage.getTextMap());

        weChatMessageReqDTO.setMarkdown(markdown);

        // 全局入参赋值
        sendMessageReqDTO.setSendType(SendTypeEnum.WE_CHAT.getName());
        sendMessageReqDTO.setWeChatMessageReqDTO(weChatMessageReqDTO);

        return sendMessageReqDTO;
    }
}
